#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "noti.h"

typedef __uint32_t uint32;

int main(int argc, char** args)
{
  
  int sock;
  struct sockaddr_in addr;

  char * sTitle;
  char * sText;
  uint32 sTime = 10;
  
  if(argc<=1)
    {
      printf("Need arguments.\n");
      exit(1);
    }else
    {
      int ai = 1;
      while(ai<argc)
	{
	  
	  if(strcmp(args[ai],"-title") == 0)
	    {
	      sTitle = args[++ai];
	      printf("Title - %s\n",sTitle);
	    } else if(strcmp(args[ai],"-text") == 0)
	    {
	      sText = args[++ai];
	      printf("Text - %s\n",sText);
	    } else if(strcmp(args[ai],"-time") == 0)
	    {
	      sTime = atoi(args[++ai]);
	      printf("Time - %u\n",sTime);
	    } //else if(strcmp(args[ai],"-text") == 0)


	    ai++;
	}
      
      
    }
  
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if(sock < 0)
    {
      perror("socket");
      exit(1);
    }
  
  addr.sin_family = AF_INET;
  addr.sin_port = htons(3425); // или любой другой порт...
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
      perror("connect");
      exit(2);
    }
  
  noti::RecType rt = noti::Message;
  uint32 pc = 3; //parameters count
  send(sock, &rt, sizeof(noti::RecType), 0);
  send(sock, &pc, sizeof(uint32), 0);
  
  noti::RecMessFields rmf = noti::Title;
  send(sock, &rmf, sizeof(noti::RecMessFields), 0);
  char * str = (char*)sTitle;
  uint32 slen = strlen(str);
  printf("Sending title %s (%d)\n",str,slen);
  send(sock, &slen, sizeof(uint32), 0);    
  send(sock, str, slen, 0);    
  
  rmf = noti::Body;
  send(sock, &rmf, sizeof(noti::RecMessFields), 0);
  str = (char*)sText;
  slen = strlen(str);
  send(sock, &slen, sizeof(uint32), 0);    
  send(sock, str, slen, 0);
  
  rmf = noti::Time;
  send(sock, &rmf, sizeof(noti::RecMessFields), 0);
  uint32 num = sTime;
  send(sock, &num, sizeof(uint32), 0);
  
    
  close(sock);
  
  return 1;
}
